package com.daocao.hua.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.daocao.hua.R;
import com.daocao.hua.adapter.V3MyBorrowRecordAdapter;
import com.daocao.hua.adapter.V3MyRepayRecordAdapter;
import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.CodeBean;
import com.daocao.hua.api.bean.PageBean;
import com.daocao.hua.constant.UserGlobal;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.OnLoadeMoreListener;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;
import com.daocao.hua.util.Util;
import com.daocao.hua.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3MyHistoryBillActivity extends BaseActivity implements V3MyRepayRecordAdapter.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    private Unbinder unbinder;
    private V3MyRepayRecordAdapter adapter;

    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getPaymentNotice(page);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_borrow_record_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
        getPaymentNotice(1);
    }

    public void getPaymentNotice(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        PageBean bean = new PageBean();
        bean.page = page;
        Api.api_service.getPaymentNotice(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {

                });
    }

    private void init() {
        txtTitle.setText("历史账单");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getPaymentNotice(1);
            }
        });
        adapter = new V3MyRepayRecordAdapter(this);
        adapter.setOnChooseListener(this);
        list.addOnScrollListener(onLoadeMoreListener);
        list.setAdapter(adapter);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onClickHetong(String dealId) {
        Intent intent = new Intent(this, LoanAgreementActivity.class);
        intent.putExtra("type", 0);
        intent.putExtra("deal_id", dealId);
        UserGlobal.info.deal_id = dealId;
        UserGlobal.info.type = 0;
        startActivity(intent);
        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    @Override
    public void onClickWechat() {
        Util.startActivity(this, WechatChatActivity.class);
    }
}
